$(function () {
    $("#queryhistory-tab").addClass('active');

    //$('#idHistoryTab a').click(function (e) {
    //    e.preventDefault()
    //    $(this).tab('show')
    //})
    //
    var logisticsQueryParams = {"pageSize": 20, "currentPage": 1, "params": {"queryType": 1}};
    var getLogisticsQueryHistory = function (queryParams) {
        $.ajax({
            url: ctx + 'queryHistory/histories',
            type: 'post',
            contentType: 'application/json;charset=UTF-8',
            dataType: 'json',
            data: JSON.stringify(queryParams),
            success: function (result, textStatus, jqXHR) {
                if (result.status == 0) {
                    console.log(result);
                    var data = result.data;
                    var htmlStr = "";
                    $.each(data,function(index, item)
                    {
                        htmlStr += " <tr>"
                            + "<td>" + item.waybillNo + "</td>"
                            + "<td>" + item.comName + "</td>"
                            + "<td>" + item.queryTime + "</td>"
                            + "<td>"
                            + "<button class='btn btn-danger' type='button'>删除</button>"
                            + "<button class='btn btn-default' type='button'>跟踪记录</button>"
                            + "</td>"
                            + "  </tr>";
                    });


                    $('#idLogisticsQueryHistoryTable tbody').html(htmlStr);
                    showPagination(result.pageSize,result.currentPage,result.totalSize,$("#idLogisticsQueryHistoryPagination"),function(pageIndex){
                        logisticsQueryParams["currentPage"] = pageIndex;
                        getLogisticsQueryHistory(logisticsQueryParams);
                    });
                }
            }
        });
    };

    getLogisticsQueryHistory(logisticsQueryParams);


    $("#idLogisticsQueryForm").submit(function(e){
        e.preventDefault();

        $(this).find("input").each(function(index,item){
            var val =  $(this).val()
            var name = $(this).attr("name");
            if(val!=""){
                logisticsQueryParams["params"][name] = val;
            }else{
               delete logisticsQueryParams["params"][name];
            }

        });
        logisticsQueryParams["currentPage"] = 1;
        getLogisticsQueryHistory(logisticsQueryParams);
    });

    //-----------------------------
    //快递
    var exressQueryParams = {"pageSize": 20, "currentPage": 1, "params": {"queryType": 2}};
    var getExpressQueryHistory = function (queryParams) {
        $.ajax({
            url: ctx + 'queryHistory/histories',
            type: 'post',
            contentType: 'application/json;charset=UTF-8',
            dataType: 'json',
            data: JSON.stringify(queryParams),
            success: function (result, textStatus, jqXHR) {
                if (result.status == 0) {
                    console.log(result);
                    var data = result.data;
                    var htmlStr = "";
                    $.each(data,function(index, item)
                    {
                        htmlStr += " <tr>"
                            + "<td>" + item.waybillNo + "</td>"
                            + "<td>" + item.comName + "</td>"
                            + "<td>" + item.queryTime + "</td>"
                            + "<td>"
                            + "<button class='btn btn-danger' type='button'>删除</button>"
                            + "<button class='btn btn-default' type='button'>跟踪记录</button>"
                            + "</td>"
                            + "  </tr>";
                    });


                    $('#idExpressQueryHistoryTable tbody').html(htmlStr);
                    showPagination(result.pageSize,result.currentPage,result.totalSize,$("#idExpressQueryHistoryPagination"),function(pageIndex){
                        exressQueryParams["currentPage"] = pageIndex;
                        getExpressQueryHistory(exressQueryParams);
                    });
                }
            }
        });
    };

    getExpressQueryHistory(exressQueryParams);




    //分页
    showPagination = function(pageSize,currentPage,totalSize,target,action){
        //1 最多显示5项
        //2 向左向左显示上一页一下页
        //3.当页号是最后一项时点显示以当前页为中间项的
        var totalPages = Math.ceil(totalSize/pageSize);
        var maxItems = 5;
        var startIndex = 1;
        var endIndex = 1;
        if(totalPages <= maxItems){
            startIndex = 1;
            endIndex = totalPages;
        }else{
            if(currentPage <maxItems-1){
                startIndex =1
                endIndex = maxItems
            }else{
                startIndex = currentPage - Math.ceil(maxItems/2);
                endIndex = currentPage + maxItems-Math.ceil(maxItems/2)-1;
            }
        }

        var html = '';
        //回到第一页按钮
        if(currentPage<=1){
            html+='<li class="disabled "><a href="javascript:void(0)">&laquo;</a></li>';
        }else{
            html+='<li><a class="pagination-first-page" href="javascript:void(0)">&laquo;</a></li>';
        }
        //上一页按钮
        if(currentPage<=1){
            html+='<li class="disabled "><a href="javascript:void(0)">&lsaquo;</a></li>';
        }else{
            html+='<li><a class="pagination-previous-page" href="javascript:void(0)">&lsaquo;</a></li>';
        }
        for(var i=startIndex;i<=endIndex;i++){
            if(i==currentPage){
                html +='<li class="active"><a data-my-page-index="'+i+'" href="javascript:void(0)">'+i+'</a></li>';
            }else{
                html +='<li><a  class="pagination-index" data-my-page-index="'+i+'" href="javascript:void(0)">'+i+'</a></li>';
            }

        }


        //下一页
        if(currentPage==totalPages){
            html += '<li class="disabled" ><a href="javascript:void(0)">&rsaquo;</a></li>'
                + '<li class="disabled"><a  href="javascript:void(0)">&raquo;</a></li>';
        }else{
            html += '<li><a class="pagination-next-page" href="javascript:void(0)">&rsaquo;</a></li>'
                + '<li><a  class="pagination-last-page" href="javascript:void(0)">&raquo;</a></li>';
        }


        $(target).html(html);
        $(target).find('a[class="pagination-index"]').click(function(e){
            //$(target).find('li').removeClass('active');
            //$(this).parent('li').addClass('active');
            var pageIndex =  $(this).data("my-page-index");
            action(pageIndex);
        });


        $(target).find('a[class="pagination-first-page"]').click(function(e){
            //第一页
            action(1);
        });
        $(target).find('a[class="pagination-previous-page"]').click(function(e){
            //上一页
            action(currentPage-1);
        });
        $(target).find('a[class="pagination-last-page"]').click(function(e){
            action(totalPages)
            //最后一页
        });
        $(target).find('a[class="pagination-next-page"]').click(function(e){
            //下一页
            action(currentPage+1);
        });

    };


    //日期选择器
    $("#idLogisticsStartDate").datetimepicker({
        format: 'yyyy-mm-dd hh:ii',
        language: 'zh-CN',
        autoclose:true

    });

    //日期选择器
    $("#idLogisticsEndDate").datetimepicker({
        format: 'yyyy-mm-dd hh:ii',
        language: 'zh-CN',
        autoclose:true

    });

    //日期选择器
    $("#idExpressStartDate").datetimepicker({
        format: 'yyyy-mm-dd hh:ii',
        language: 'zh-CN',
        autoclose:true

    });

    //日期选择器
    $("#idExpressEndDate").datetimepicker({
        format: 'yyyy-mm-dd hh:ii',
        language: 'zh-CN',
        autoclose:true

    });
})